package han.fetch;

import han.entity.FinanceProjectInfoEntity;
import han.entity.InvestCompanyIdEntity;
import han.entity.InvestCompanyInfoEntity;
import han.parser.Regex;
import han.proof.Cookie;
import han.proof.CookieRepository;
import han.sql.InsertThread;
import han.sql.Operator;
import han.thread.CustomThread;
import han.thread.MonitorThread;
import han.util.ListTool;

import java.util.*;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @author: Han
 * @date: 2018/7/12 13:53
 * @dscription: 线程入口, 抓取投资机构
 */
class InvestOrganizationInfo {

    private CookieRepository cookieRepository;

    private Operator operator;

    private Object krLock = new Object();

    InvestOrganizationInfo(CookieRepository cookieRepository) {
        this.cookieRepository = cookieRepository;
        operator = Operator.getInstance();
    }

    /**
     * 获取投资机构信息
     */
    void threadStart() {

        //已抓取到投资机构信息
        List<InvestCompanyInfoEntity> fetchedList = operator.queryAllCompanyInfo();
        //待抓取投资机构id
        List<InvestCompanyIdEntity> idEntityList = operator.queryAllInvestCompanyId();
        //融资机构中的投资机构id
        List<InvestCompanyIdEntity> financeInvestIdList = getOrgIdFromProjectInfo();
        //添加到待抓取id
        idEntityList.addAll(financeInvestIdList);
        //去重后的id list
        List<InvestCompanyIdEntity> fetchingList = deleteRepeatId(idEntityList, fetchedList);
        //分开list
        List<List<InvestCompanyIdEntity>> splitIdEntityList = new ListTool<InvestCompanyIdEntity>().splitListByCount
                (fetchingList, 50);
        //cookie list
        List<Cookie> cookieList = cookieRepository.getCookieList();
        ThreadPoolExecutor executor = new CustomThread();
        InsertThread insertThread = new InsertThread();
        executor.submit(insertThread);
        MonitorThread monitorThread = new MonitorThread(insertThread,executor,cookieRepository);
        executor.submit(monitorThread);
        for (int index = 0; index < splitIdEntityList.size(); index++) {

            List<InvestCompanyIdEntity> companyIdEntityList = splitIdEntityList.get(index);
            Cookie cookie = cookieList.get(index);
            InvestOrganizationInfoThread investOrganizationInfoThread = new InvestOrganizationInfoThread
                    (companyIdEntityList, krLock, cookie, insertThread);
            executor.submit(investOrganizationInfoThread);
            try {
                //每过一段时间,提交一次任务
                Thread.sleep(1000);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        executor.shutdown();
    }

    /**
     * 获取融资项目融资经历中的投资机构id
     *
     * @return 投资机构id
     */
    private List<InvestCompanyIdEntity> getOrgIdFromProjectInfo() {
        //投资机构id容器
        HashSet<String> orgIdSet = new HashSet<>();
        //获取所有融资项目
        List<FinanceProjectInfoEntity> projectInfoEntityList = operator.queryProjectInfoByCondition(0);
        //遍历融资项目
        for (FinanceProjectInfoEntity projectInfoEntity : projectInfoEntityList) {
            //获取融资记录
            String tempFinance = projectInfoEntity.getFinanceExperience();
            //如果融资记录不存在,下一个
            if (tempFinance.length() < 5) {
                continue;
            }
            //分开每一轮融资记录
            String[] financeExperiences = tempFinance.split("\n");
            for (String financeExperience : financeExperiences) {
                //解析出投资方信息
                String tempInvestor = Regex.matchString(financeExperience, "(?<=投资方:<).*(?=>,日期:)");
                String[] investors = tempInvestor.split(">,<");
                //遍历投资方信息
                for (String investor : investors) {
                    investor = "<" + investor + ">,";
                    //解析出投资机构id,不抓取个人投资人
                    String type = Regex.matchString(investor,"(?<=,类型:).*(?=,id)");
                    if ("INDIVIDUAL".equals(type) || "COMPANY".equals(type)){
                        continue;
                    }
                    String investorId = Regex.matchString(investor, "(?<=,id:).*(?=>,)");
                    orgIdSet.add(investorId);
                }
            }
        }
        List<InvestCompanyIdEntity> fetchingList = new ArrayList<>();
        for (String orgId : orgIdSet) {
            InvestCompanyIdEntity entity = new InvestCompanyIdEntity();
            entity.setCompanyId(orgId);
            fetchingList.add(entity);
        }
        return fetchingList;
    }

    /**
     * 删除待抓取id list中存在的已抓取id
     *
     * @param fetchingIdCol 待抓取list
     * @param fetchedIdCol  已抓取list
     * @return 去重后的list
     */
    private List<InvestCompanyIdEntity> deleteRepeatId(Collection<InvestCompanyIdEntity> fetchingIdCol,
                                                       Collection<InvestCompanyInfoEntity> fetchedIdCol) {
        HashSet<String> fetchedIdSet = new HashSet<>();
        for (InvestCompanyInfoEntity fetchedEntity : fetchedIdCol) {
            String fetchedId = fetchedEntity.getKrId();
            fetchedIdSet.add(fetchedId);
        }

        HashSet<String> fetchingIdSet = new HashSet<>();
        for (InvestCompanyIdEntity fetchingEntity : fetchingIdCol) {
            String fetchingId = fetchingEntity.getCompanyId();
            fetchingIdSet.add(fetchingId);
        }
        //清空待抓取id entity list
        fetchingIdCol.clear();
        //移除待抓取id list中存在的已抓取id
        fetchingIdSet.removeAll(fetchedIdSet);
        for (String id : fetchingIdSet) {
            if ("0".equals(id)) {
                continue;
            }
            InvestCompanyIdEntity entity = new InvestCompanyIdEntity();
            entity.setCompanyId(id);
            fetchingIdCol.add(entity);
        }
        return new ArrayList<>(fetchingIdCol);
    }
}
